//
//  BELoginController
//  BEGlobalOiler
//
//  Created by KKING on 16/9/2.
//  Copyright © 2016年 Bullseye Energy pte ltd. All rights reserved.
//

import UIKit


class BELoginController: UIViewController {
    
    //测试代码
    @IBAction func test(sender: AnyObject) {
        accountTF.text = "18670386100"
        passwordTF.text = "123456"
    }
    
    @IBOutlet weak var accountTF: UITextField!
    @IBOutlet weak var passwordTF: UITextField!
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.isNavigationBarHidden = true
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //占位文字颜色初始化
        let accountTFStr = NSAttributedString(string: "User ID or Email", attributes: [NSForegroundColorAttributeName:UIColor.colorWithHexString(hex: "#676c7d")])
        accountTF.attributedPlaceholder = accountTFStr
        
        let passwordTFStr = NSAttributedString(string: "Password", attributes: [NSForegroundColorAttributeName:UIColor.colorWithHexString(hex: "#676c7d")])
        passwordTF.attributedPlaceholder = passwordTFStr
        
    }
    
    
    @IBAction func loginButtonDidClcik(sender: AnyObject) {
        
        if (accountTF.text?.characters.count)! < 5 {
            JFProgressHUD.showErrorWithStatus(status: "请输入正确的帐号")
            return
        }else if (passwordTF.text?.characters.count)! < 5 {
            JFProgressHUD.showErrorWithStatus(status: "请输入密码")
            return
        }
        view.endEditing(true)
        JFProgressHUD.show()
        showNetworkActivity()
        BEAccountModel.normalAccountLogin(username: accountTF.text!, password: passwordTF.text!) { (success, tip) in
            if success {
                JFProgressHUD.showSuccessWithStatus(status: tip)
                UserDefaults.standard.set(self.accountTF.text, forKey: "username")
                UserDefaults.standard.set(self.passwordTF.text, forKey: "password")
                
                self.dismiss(animated: true, completion: nil)
                
            } else {
                JFProgressHUD.showInfoWithStatus(status: tip)
            }
        }
    }
    
    @IBAction func forgetPwdButtonDidClick(sender: AnyObject) {
        navigationController?.pushViewController(BEForgetPwdVcController(), animated: true)
    }
    
    @IBAction func dismissLoginVc(sender: AnyObject) {
        view.endEditing(true)
        dismiss(animated: true, completion: nil)
    }

}

